import Util from '../libs/util'
import Axios from '../libs/axios'

export default {
	state: {

	},
	getters: {
		// 获取导航当前信息
		getPageInfo: (state) => (id) => {
			let result = state.nav.filter(item => (item.channelIds == id))
			if (result.length > 0) {
				return result[0]
			} else {
				return {}
			}
		}
	},
	mutations: {
		SET_AUTHCODE(state, data) {
			state.authCode = data
		},
        // 注册
        SET_registerPage_saveData(state, data) {
			state.register.payload = {
                ...data,
            }
		},
		SET_registerPage_changeStatus(state, data) {
			state.register[data.step] = {
                disabled:data.value
            }
		},
		// 登陆
		SET_loginpage_login_status(state, data) {
			// state.login.status = data.status
			// state.login.msg = data.msg
			state.login = {
				...data
			}
		},
	},
	actions: {
		// 登陆
		loginPage_login_action({
			commit
		}, paramsObj) {
			commit('SET_loginpage_login_status', {
				status: 'loading',
				msg: ''
			})
			return new Promise((resolve, reject) => {
				let url = Util.domain + paramsObj.type == 'login'
					? '/credit-data-xypj/xypjUser/login.json'
					: '/credit-data-xypj/xypjUser/logout.json'

				Axios.axiosInstance.post(url, paramsObj.data).then((response) => {
					// 判断操作类型
					if (paramsObj.type == 'login') {
						commit('SET_loginpage_login_status', {
							status: 'success',
							msg: '登陆成功'
						})
					} else {
						commit('SET_loginpage_login_status', {
							status: 'success',
							msg: '登出成功'
						})
					}

					resolve(response)
				}).catch((err) => {
					console.log(err);
					commit('SET_loginpage_login_status', {
						status: 'error',
						msg: '接口异常'
					})

					reject(err)
				})
			})
		},
        registerPage_saveData_action({commit},paramsObj) {
            if(paramsObj.type == 'save'){
                commit('SET_registerPage_saveData',paramsObj.data)
            }else{
                commit('SET_registerPage_changeStatus',paramsObj.data)
            }
		},
	}
};
